Skip to content

fix: support persistent lifespans in asgi - #231

Open
dom96 wants to merge 1 commit into
mainfrom
dominik/persistent-lifespan
Open

fix: support persistent lifespans in asgi#231
dom96 wants to merge 1 commit into
mainfrom
dominik/persistent-lifespan

Conversation

@dom96

@dom96 dom96 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Ensures that mutable lifespans are persisted across requests. Adds fastapi and asgi-specific tests.

@dom96
dom96 requested review from hoodmane and ryanking13 August 27, 2026 17:19
Comment thread packages/runtime-sdk/src/workers/asgi.py Outdated
@ask-bonk

ask-bonk Bot commented Aug 27, 2026

Copy link
Copy Markdown

I'm Bonk, and I've done a quick review of your PR.

Makes ASGI entrypoint lifespan state persist across requests.

  1. High: Posted 1 actionable inline suggestion.

github run

Comment thread packages/runtime-sdk/src/workers/asgi.py Outdated
Comment thread packages/runtime-sdk/src/workers/asgi.py Outdated
@dom96
dom96 force-pushed the dominik/persistent-lifespan branch 2 times, most recently from 864cb45 to 5b8cb5e Compare August 28, 2026 16:00
Comment thread packages/runtime-sdk/src/workers/asgi.py
Comment thread packages/runtime-sdk/src/workers/asgi.py Outdated

@hoodmane hoodmane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks reasonable, though I'd set _start_future on the AsgiWorkerEntrypoint instance rather than the class and initialize _start_future to None in AsgiWorkerEntrypoint.__init__(). Could adjust it in a followup if you like though.

@dom96
dom96 force-pushed the dominik/persistent-lifespan branch 2 times, most recently from 8b7b84c to afea9e1 Compare September 1, 2026 16:29
@dom96

dom96 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

So we do actually want it set on the class because the instance is re-created on every worker invocation. The lifespan is supposed to stay alive for the duration of the lifetime of the isolate. I added a comment to explain this.

@hoodmane

hoodmane commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

the instance is re-created on every worker invocation

That is really weird, can we migrate away from that?

@hoodmane

hoodmane commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

In JavaScript is the WorkerEntrypoint instantiated on every request?

@dom96

dom96 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Yes, according to https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/rpc/#the-workerentrypoint-class

A new instance of the class is created every time the Worker is called.

@dom96
dom96 force-pushed the dominik/persistent-lifespan branch from afea9e1 to 9beb25b Compare September 2, 2026 17:59
@hoodmane

hoodmane commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Right it also says:

Note that even though the Worker is implemented as a class, it is still stateless — the class instance only lasts for the duration of the invocation. If you need to persist or coordinate state in Workers, you should use Durable Objects.

What is this persistent lifespan state being used for? Is it volatile cache kind of stuff? Would be interested if you could post the use case.

@dom96

dom96 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

As far as I understand it's for reconstructible event-loop-local resources like HTTP clients, database pools, etc. Perhaps it isn't all that useful for Workers, but existing FastAPI apps that wish to run on Workers may find it useful. It's just about keeping things that are reused by each requested handled inside a single isolate.

If you don't think it's worth the complexity that's fine too. We can close :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants